/******************************************************************************************
 * Import
 ******************************************************************************************/

// module
import Taro, { Component } from '@tarojs/taro'

// project
import { CPage, CContent, CHeader, CFooter, CFloat, CAround, CBetween, CCenter, CRow, CWrap, CLine, CHand, CItemX, CItemY, CText, CLabel, CSvg, CImage, CButton, CColumn } from '../../../library/comp';
import { Base64, Gen, Time, Zip, Session, Storage, Http, Toast} from '../../../library/func';
import Icon from '../../../project/style/icon';
import ConfigNet from '../../func/http/http';


/******************************************************************************************
 * Export Class
 ******************************************************************************************/

export default class ProductList extends Component {

	/*------------------------------------------------------------------------------------
	 * 初始化
	 *-----------------------------------------------------------------------------------*/
	constructor(props) {
		super(props);

		/**
		 * state
		 */
		this.state = {
			
		}
	}

	/*------------------------------------------------------------------------------------
	 * 生命周期
	 *-----------------------------------------------------------------------------------*/
	// didMount
	componentDidMount() {

	}
	
	/*------------------------------------------------------------------------------------
	 * 事件处理
	 *-----------------------------------------------------------------------------------*/
	// onRedirect
    onRedirect = (url, e)=> {
        wx.redirectTo({
            url: url
        })
    }

    // onNavigate
    onNavigate = (url, e)=> {
		wx.navigateTo({
			url: url
		})
	}


	//加入购物车
	subCart(obj, e){
		ConfigNet.post({
			url:'cart/logic/add.php',
			data:{
				pid:obj.id,
				cid1:obj.fk_cid1,
				cid2:obj.fk_cid2,
				count:1,
			},
			success:(res) => {
				console.log(res)
				Toast.success("添加到购物车");
			},error(res){
				Toast.text(res.msg);
			}
		});
	}


	/*------------------------------------------------------------------------------------
	 * 自定义方法
	 *-----------------------------------------------------------------------------------*/

	/*------------------------------------------------------------------------------------
	 * 渲染
	 *-----------------------------------------------------------------------------------*/
	render () {
		return (
			<CColumn bg='#fff' pa='20' pt='35'>
				<CColumn>
				{this.props.data.map((item,index) => {
					return (
						<CColumn style='box-shadow: 0 2px 20px #d6d6d6; border-radius: 30rpx; margin-bottom: 30rpx'>
							{/* 图片 */}
							<CLine onClick={ this.onNavigate.bind(this, '/screen/product/detail?id=' + item.id) }>
								<Image lazyLoad={true} src={'http://image.cdn.hesq.com.cn/rushbuy/product/'+ item.object.set_image_home} style='width:750rpx; height:300rpx;border-radius:30rpx 30rpx 0 0;'/>
							</CLine>

							{/* 文本 */}
							<CColumn pt='40' pb='30' px='40'>
								<CLine y='top'>
									<CLabel bg='#fdf9ec' color='#cfb583' size='20' w='100'>今日秒杀</CLabel>
									<CText pl='10' size='30' style='font-weight: bold'>{item.set_name}</CText>
								</CLine>
								<CBetween>
									<CLine>
										<CItemY py='15' x='left'>
											<CHand y='bottom'>
												<CHand>
													<CText size='32'>￥</CText>
													<CText size='32' style='font-weight: bold'>{item.set_price}</CText>
												</CHand>
												{
													this.props.isvip == 1 ?
														(
															<CHand ml='32'>
																<CText color='#c72e24' size='34'>赚</CText>
																<CText color='#c72e24' size='36'>￥</CText>
																<CText color='#c72e24' size='36'>{Math.floor(item.set_price*item.set_commission_percent)/100}</CText>
															</CHand>
														)
														:
														(
															<CHand style='font-size:30rpx; text-decoration: line-through;color:#999; margin-left:10rpx'>￥{item.set_original_price}</CHand>
														)
													
													
												}
												
											</CHand>
											<CText color='#afafaf' mt='12' size='25'>销量{item.count_sale}</CText>
										</CItemY>
									</CLine>
									<CLine x='right'>
										<CSvg onClick={ this.subCart.bind(this, item) } icon={Icon.cart} w='60' h='60' mr='30'></CSvg>
										<CSvg icon={Icon.share} w='40' h='40' color='#666'></CSvg>
									</CLine>
								</CBetween>
							</CColumn>
						</CColumn>
					)
				})}
				</CColumn>
            </CColumn>
		)
	}
}


